package cn.dxc.demo.controller;


import cn.dxc.demo.common.RandomCode;
import cn.dxc.demo.common.RegxUtils;
import cn.dxc.demo.common.ResultMapUtil;
import cn.dxc.demo.common.VerifyCodeUtil;
import cn.dxc.demo.entity.User;
import cn.dxc.demo.service.IUserService;
import cn.dxc.demo.vo.RegisterVO;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Api(tags = "用户接口")
@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    // 验证码
    private String code;

    @ApiOperation("注册")
    @PostMapping("/toRegister")
    public Object register(@RequestBody() RegisterVO user){

        String phone = user.getPhone();
        String password = user.getPassword();
        String username = user.getUsername();
        String company_name = user.getCompanyName();

        if (phone == null || password == null) {
            return ResultMapUtil.getHashMapInfo("用户名或密码为空", "501", "失败");
        }

        if (username == null || company_name == null) {
            return ResultMapUtil.getHashMapInfo("公司名或用户名不可为空", "502", "失败");
        }

        if (RegxUtils.isMobile(phone) == false){
            return ResultMapUtil.getHashMapInfo("验证码不正确", "503", "无效的手机号");
        }

        try{
            final String msg = userService.insertUserForRegister(phone, password,username,company_name);
            if ("500".equals(msg)){
                return ResultMapUtil.getHashMapInfo("手机号已经存在","500","失败");
            }else if ("405".equals(msg)){
                return ResultMapUtil.getHashMapInfo("用户名已经存在","405","失败");
            }else {
                return ResultMapUtil.getHashMapInfo(msg,"200","成功");
            }
        }catch (DataAccessException e){
            return ResultMapUtil.getHashMapInfo("网络出错了","0","失败");
        }

    }

    @ApiOperation("手机号查询用户")
    @PostMapping("/getUser")
    public Object getUserById(@ApiParam("手机号/邮箱") @RequestParam("general") String general){

        try{
            final User user = userService.findUserByPhone(general);
            if (user != null){
                return ResultMapUtil.getHashMapInfo(user,"1","信息获取成功");
            }else {
                return ResultMapUtil.getHashMapInfo("用户不存在","0","失败");
            }
        }catch (DataAccessException e){
            return ResultMapUtil.getHashMapInfo("网络出错了","0","失败");
        }
    }

    @ApiOperation("忘记密码")
    @PostMapping("/alterPassword")
    @ApiImplicitParams({@ApiImplicitParam(name = "phone",required = false,value = "手机号码"),
            @ApiImplicitParam(name = "email",required = false,value = "邮箱"),
            @ApiImplicitParam(name = "password",required = true)})
    public Object updatePassword(@RequestBody  User user){

        try{
            final Integer msg = userService.updateUserPassword(user);
            if (msg == 1){
                return ResultMapUtil.getHashMapInfo(null,"1","成功");
            }else {
                return ResultMapUtil.getHashMapInfo(null,"0","失败");
            }

        }catch (DataAccessException e){
            return ResultMapUtil.getHashMapInfo("网络出错了","0","失败");
        }


    }

    @GetMapping(value = "/verify")
    public Object verifyCode(@ApiParam("手机号码") @RequestParam String phone){
        code = RandomCode.getCode();
        final HashMap<String,Object> map = (HashMap)VerifyCodeUtil.verifySend(phone, code);
        return ResultMapUtil.getHashMapInfo(this.code,map.get("code"),map.get("data"));
    }














}
